<?php

namespace app\admin\model;

use think\Model;

class Rooms extends Model
{
    /**
     * 删除
     */
    public function DeleteById($id) {
        if ($this->where('id', $id)->count() == 1) {
            $this->where('id', $id)->delete();

            return Result(1, '删除成功');
        }

        return Result(0, '提供的数据id不正确');
    }

    /**
     * 创建房间
     */
    public function Created($option, $images)
    {
        if (count($option) === 0) {
            return Result(0, '数据填写不完整');
        }

        if (!array_key_exists('region_id', $option) && db('region')->where('id', $option['region_id'])->count() === 0) {
            return Result(0, '地区不存在');
        }

        if (!array_key_exists('specifications_id', $option) && db('specifications')->where('id', $option['specifications_id'])->count() === 0) {
            return Result(0, '该规格不存在');
        }
        
        $id = $this->insertGetId($option);
        trace($id);
        if (count($images) !== 0) {
            $data = [];
            foreach ($images as $key => $value) {
                $data[$key]['url'] = $value;
                $data[$key]['rooms_id'] = $id;
            }
            db('rooms_img')->insertAll($data);
        }

        return Result(1, '添加成功', [
            'id' => $id
        ]);
    }

    /**
     * 获取所有房间列表
     */
    public function GetList($page, $option)
    {
        // 创建匿名函数 方便多次调用 用完自动销毁
        $buildDb = function () use ($option) {
            $db = $this
                    ->alias('r')
                    ->field('r.*, s.name specifications_name, re.name region_name')
                    ->join('specifications s', 'r.specifications_id = s.id', 'LEFT')
                    ->join('region re', 'r.region_id = re.id', 'LEFT')
                    ->where('r.state', 1);
            // -start 查询条件
            // 根据价格范围查询
            if (array_key_exists('max_price', $option) && array_key_exists('min_price', $option)) {
                $db = $db->whereBetween('r.price', [$option['min_price'], $option['max_price']]);
            }
            // 根据地区查询
            if (array_key_exists('region_id', $option) && !empty($option['region_id'])) {
                $regionArray = db('region', [], false)->where('parent_id', $option['region_id'])->column('id');

                $regionArray[] = $option['region_id'];
                
                $db = $db->whereIn('r.region_id',  $regionArray);
            }
            // 根据地区名称查询
            if (array_key_exists('region_name', $option) && !empty($option['region_name'])) {
                $regionid = db('region', [], false)->where('name', $option['region_name'])->value('id');
                if ($regionid) {
                    $regionArray = db('region', [], false)->where('parent_id', $regionid)->column('id');
                    $regionArray[] = $regionid;

                    $db = $db->whereIn('r.region_id', $regionArray);
                }
            }
            // 根据规格查询
            if (array_key_exists('specifications_id', $option) && !empty($option['specifications_id'])) {
                $db = $db->where('r.specifications_id', $option['specifications_id']);
            }
            // 根据规格名称查询
            if (array_key_exists('specifications_name', $option) && !empty($option['specifications_name'])) {
                $db = $db->whereLike('s.name', '%'.$option['specifications_name'].'%');
            }
            // 根据房间联系手机号查询
            if (array_key_exists('mobile', $option) && !empty($option['mobile'])) {
                $db = $db->where('r.mobile', $option['mobile']);
            }
            // 根据房间名称查询
            if (array_key_exists('name', $option) && !empty($option['name'])) {
                $db = $db->whereLike('r.name', '%'.$option['name'].'%');
            }
            // -end
            return $db;
        };

        $count = $buildDb()->count();
        $list = $buildDb()->page($page, 50)->select();

        return Result(1, '', [
            'count' => $count,
            'pageSize' => 50,
            'list' => $list
        ]);
    }

    /**
     * 获取房间信息
     */
    public function GetInfo($id)
    {
        if ($this->where('id', $id)->count() == 0) {
            return Result(0, '该房间不存在');
        }
        $info = $this
                ->alias('r')
                ->field('r.*, s.name specifications_name, re.name region_name')
                ->join('specifications s', 'r.specifications_id = s.id', 'LEFT')
                ->join('region re', 'r.region_id = re.id', 'LEFT')
                ->where('r.id', $id)
                ->where('r.state', 1)
                ->find();

        $info['images'] = db('rooms_img')->where('rooms_id', $id)->select();

        return Result(1, '', [
            'info' => $info
        ]);
    }

    /**
     * 修改房间信息
     */
    public function ModifyData($id, $option, $images)
    {
        if ($this->where('id', $id)->count() == 0) {
            return Result(0, '该房间不存在');
        }

        $this->where('id', $id)->data($option)->update();

        if (count($images) !== 0) {
            db('rooms_img', [], false)->where('rooms_id', $id)->delete();
            $data = [];
            foreach ($images as $key => $value) {
                $data[]['url'] = $value;
                $data[]['rooms_id'] = $id;
            }
            db('rooms_img')->insertAll($data);
        }

        return Result(1, '修改成功');
    }

    /**
     * 获取指定房间的图片
     */
    public function GetImageList($id) 
    {
        $list = db('rooms_img')->where('rooms_id', $id)->select();

        return Result(1, '', [
            'list' => $list
        ]);
    }

    /**
     * 保存图片
     */
    public function CreateImage($roomsId, $url)
    {
        $id = db('rooms_img')->insertGetId([
            'rooms_id' => $roomsId,
            'url' => $url
        ]);

        return Result(1, '', [
            'id' => $id
        ]);
    }

    /**
     * 删除图片
     */
    public function DelImage($id)
    {
        if(db('rooms_img', [], false)->where('id', $id)->count() == 1) {
            db('rooms_img', [], false)->where('id', $id)->delete();
            return Result(1, '删除成功');
        }

        return Result(0, '删除失败');
    }

    /**
     * 查询指定条件的单个房间信息
     */
    public function FindRooms($type, $account)
    {
        $rooms = $info = $this
        ->alias('r')
        ->field('r.*, s.name specifications_name, re.name region_name')
        ->join('specifications s', 'r.specifications_id = s.id', 'LEFT')
        ->join('region re', 'r.region_id = re.id', 'LEFT')
        ->where('r.state', 1)
        ->where("r.{$type}", "{$account}")->find();

        if (!$rooms) {
            return Result(0, '该房间不存在');
        }
        return Result(1, '', [
            'rooms' => $rooms
        ]);
    }
}
